﻿using Autofac;
using DBContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Common;
using ZoneLine.Models;
using ZoneLine.Models.PrintModeConfig;

namespace Common
{
    public class SysCfg
    {
        /// <summary>
        /// 设备类型
        /// </summary>
        public static string DBStr => ConfigurationUtil.GetConfiguration(Convert.ToString, () => "server=localhost;port=3306;User Id=root;password=root;Database=cks2407;charset=utf8; SslMode=none");
        //后减数据库IP
        //public static string DBStr => ConfigurationUtil.GetConfiguration(Convert.ToString, () => "server=172.20.23.175;port=3306;User Id=root;password=root;Database=cks2407;charset=utf8; SslMode=none");
        //前减数据库IP
        //public static string DBStr => ConfigurationUtil.GetConfiguration(Convert.ToString, () => "server=172.20.23.128;port=3306;User Id=root;password=root;Database=cks2407;charset=utf8; SslMode=none");


        /// <summary>
        /// 获箱码取物料编号
        /// </summary>
        /// <param name="opstation">1：前减 2：后减</param>
        /// <param name="currentProductType">X04、W04</param>
        /// <param name="workDirectionEnum">左右</param>
        /// <returns></returns>
        public static string GetBoxCodeMaterialCode(int opstation, string currentProductType, WorkDirectionEnum workDirectionEnum)
        {

            string materialCode = "";
            if (opstation == 1 && currentProductType.ToUpper() == "X04" && workDirectionEnum == WorkDirectionEnum.Left)//前左
            {
                materialCode = "000000004040102916";
            }
            else if (opstation == 1 && currentProductType.ToUpper() == "X04" && workDirectionEnum == WorkDirectionEnum.Right)//前右
            {
                materialCode = "000000004040102918";
            }
            else if (opstation == 2 && currentProductType.ToUpper() == "X04" && workDirectionEnum == WorkDirectionEnum.Left)//后左
            {
                materialCode = "000000004040102917";
            }
            else if (opstation == 2 && currentProductType.ToUpper() == "X04" && workDirectionEnum == WorkDirectionEnum.Right)//后右
            {
                materialCode = "000000004040102919";
            }

            if (opstation == 1 && currentProductType.ToUpper() == "W04" && workDirectionEnum == WorkDirectionEnum.Left)//前左
            {
                materialCode = "000000004040102996";
            }
            else if (opstation == 1 && currentProductType.ToUpper() == "W04" && workDirectionEnum == WorkDirectionEnum.Right)//前右
            {
                materialCode = "000000004040102997";
            }
            else if (opstation == 2 && currentProductType.ToUpper() == "W04" && workDirectionEnum == WorkDirectionEnum.Left)//后左
            {
                materialCode = "000000004040102998";
            }
            else if (opstation == 2 && currentProductType.ToUpper() == "W04" && workDirectionEnum == WorkDirectionEnum.Right)//后右
            {
                materialCode = "000000004040102999";
            }

            return materialCode;
        }


        /// <summary>
        /// 初始化标签打印配置
        /// </summary>
        /// <param name="dal"></param>
        public static void ReloadPrintConfig()
        {
            try
            {
                var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
                var dal = ContainerServer.Container.Resolve<IModelDAL>();

                var lxqcConfig = dal.GetModel<LXQC_Config>(p => p.ProductType.Equals(Common.Cache.CurrentProductType));
                if (lxqcConfig == null)
                {
                    //_dal.AddModel(new LXQC_Config());
                    lxqcConfig = new LXQC_Config();
                }
                config.LxqcConfig = lxqcConfig;

                var sqqcConfig = dal.GetModel<SQQC_Config>(p => p.ProductType.Equals(Cache.CurrentProductType));
                if (sqqcConfig == null)
                {
                    //_dal.AddModel(new SQQC_Config());
                    sqqcConfig = new SQQC_Config();
                }
                config.Sqqc_Config = sqqcConfig;

                var bqqcLHConfig = dal.GetModel<BQQC_LH_Config>(p => p.ProductType.Equals(Cache.CurrentProductType));
                if (bqqcLHConfig == null)
                {
                    //_dal.AddModel(new BQQC_LH_Config());
                    bqqcLHConfig = new BQQC_LH_Config();
                }
                config.BqqcLHConfig = bqqcLHConfig;

                var bqqcKHConfig = dal.GetModel<BQQC_KH_Config>(p => p.ProductType.Equals(Cache.CurrentProductType));
                if (bqqcKHConfig == null)
                {
                    //_dal.AddModel(new BQQC_KH_Config());
                    bqqcKHConfig = new BQQC_KH_Config();
                }
                config.BqqcKhConfig = bqqcKHConfig;

                var wlConfig = dal.GetModel<WLQCConfig>(p => p.ProductType.Equals(Cache.CurrentProductType));
                if (wlConfig == null)
                {
                    //_dal.AddModel(new WLQCConfig());
                    wlConfig = new WLQCConfig();
                }
                config.WlqcConfig = wlConfig;
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 保存所有打印信息
        /// </summary>
        public static void SavePrintConfig()
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            var dal = ContainerServer.Container.Resolve<IModelDAL>();
            try
            {
                if (!string.IsNullOrEmpty(config.LxqcConfig.ProductType))
                    dal.UpdateModelSuper<LXQC_Config>(p => p.ProductType.Equals(Cache.CurrentProductType), config.LxqcConfig);
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error("理想汽车打印配置保存失败" + ex.Message, "温馨提示");
            }

            try
            {
                if (!string.IsNullOrEmpty(config.Sqqc_Config.ProductType))
                    dal.UpdateModelSuper(p => p.ProductType.Equals(Cache.CurrentProductType), config.Sqqc_Config);
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error("上汽汽车打印配置保存失败" + ex.Message, "温馨提示");
            }

            try
            {
                if (!string.IsNullOrEmpty(config.BqqcLHConfig.ProductType))
                    dal.UpdateModelSuper(p => p.ProductType.Equals(Cache.CurrentProductType), config.BqqcLHConfig);
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error("北汽汽车螺簧打印配置保存失败" + ex.Message, "温馨提示");
            }

            try
            {
                if (!string.IsNullOrEmpty(config.BqqcKhConfig.ProductType))
                    dal.UpdateModelSuper(p => p.ProductType.Equals(Cache.CurrentProductType), config.BqqcKhConfig);
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error("北汽汽车空簧打印配置保存失败" + ex.Message, "温馨提示");
            }

            try
            {
                if (!string.IsNullOrEmpty(config.WlqcConfig.ProductType))
                    dal.UpdateModelSuper(p => p.ProductType.Equals(Cache.CurrentProductType), config.WlqcConfig);
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error("蔚来汽车打印配置保存失败" + ex.Message, "温馨提示");
            }

            try
            {
                if (!string.IsNullOrEmpty(config.PackageConfigModel.ProductType))
                    dal.UpdateModelSuper(p => p.ProductType.Equals(Cache.CurrentProductType), config.PackageConfigModel);
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error("打包打印配置保存失败" + ex.Message, "温馨提示");
            }
        }
    }




    public class FormulaItem {

        /// <summary>
        /// IP(工位)
        /// </summary>
        public string IP { get; set; }

        /// <summary>
        /// 配方编号
        /// </summary>
        public int FormulaNumber { get; set; }

        /// <summary>
        /// 产品类型
        /// </summary>
        public ModelEnum Model { get; set; }

        /// <summary>
        /// 工件方向
        /// </summary>
        public WorkDirectionEnum WorkDirection { get; set; }

        /// <summary>
        /// 产品型号X04 W04
        /// </summary>
        public string CurrentProductType { get; set; }

        /// <summary>
        /// 工位编号
        /// </summary>
        public string StationCode { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }
    }
}